package com.zuqiuzhandui.liveing.util;

import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.util.Base64;

import com.code19.library.DateUtils;
import com.code19.library.L;
import com.zuqiuzhandui.liveing.common.MyApplication;

import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;

/**
 * @author MC
 *         Description  按照七牛的规则加工字符串
 * @date 创建时间 2016-09-26
 */
public class ManageUrl {
    static String RTMPPublishDomain = "pili-publish.zuqiuzhandui.com";
    static String Hub = "zuqiuzhandui";
    static String Access_Key = "GCR-fV0fKCWWZz0c_a2CloM4W1nw_WFaHmGSZ7DH";
    static String Secret_Key = "kVX-LF5mdCKUzKwUU1iSo748R2CpRCLIf-g2_MUW";
    static String HLSPlayDomain = "pili-live-rtmp.zuqiuzhandui.com";

    /**
     * 得到推流的地址
     * @param streamKey
     * @return
     */
    public static String getManageUrl(String streamKey) {
        StringBuffer sb = new StringBuffer();
        String timeStr = String.valueOf(System.currentTimeMillis());
        Long timeLong = Long.valueOf(timeStr.substring(0, timeStr.length() - 2)) + 10 * 60 * 60 * 12;
        L.d("过期时间是" + timeLong);
        L.d("时间字符串是" + DateUtils.formatDataTime(timeLong * 100));
        sb.append("/")
                .append(Hub)
                .append("/")
                .append(streamKey)
                .append("?e=")
                .append(timeLong);
        String path = sb.toString();
        L.d("path是" + path);
        String sign = null;
        try {
            sign = hmacSha1(path, Secret_Key);
            L.d("转化的sign是" + sign);
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (InvalidKeyException e) {
            e.printStackTrace();
        }
        String token = Access_Key + ":" + sign;
        StringBuffer sb2 = new StringBuffer();
        sb2.append("\nrtmp://")
                .append(RTMPPublishDomain)
                .append("/")
                .append(Hub)
                .append("/")
                .append(streamKey)
                .append("?e=")
                .append(timeLong)
                .append("&token=")
                .append(token);
        L.d("生成的url是" + sb2.toString());

        return sb2.toString();
    }

    public static String hmacSha1(String base, String key) throws NoSuchAlgorithmException, InvalidKeyException {

        String type = "HmacSHA1";

        SecretKeySpec secret = new SecretKeySpec(key.getBytes(), type);

        Mac mac = Mac.getInstance(type);

        mac.init(secret);

        byte[] digest = mac.doFinal(base.getBytes());

        return Base64.encodeToString(digest, Base64.DEFAULT);

    }

    /**
     * 得到播放的地址
     * @param streamStr
     * @return
     */
    public static String getHLSStr(String streamStr) {
        StringBuffer stringBuffer = new StringBuffer();
        stringBuffer.append("http://")
                .append(HLSPlayDomain)
                .append("/")
                .append(Hub)
                .append("/")
                .append(streamStr)
                .append(".m3u8");
        L.d("播放地址是" + stringBuffer.toString());
        //将播放地址剪切到剪切板上
        ClipboardManager cmb = (ClipboardManager) MyApplication.getInstance().getApplicationContext().getSystemService(Context.CLIPBOARD_SERVICE);
        ClipData clipData = ClipData.newPlainText("one", stringBuffer.toString());
        cmb.setPrimaryClip(clipData);


        return stringBuffer.toString();
    }

}
